package org.zecontech.extraplus.datagen.levelgen;

import net.minecraft.core.HolderGetter;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstrapContext;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.random.Weighted;
import net.minecraft.util.random.WeightedList;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.common.world.BiomeModifier;
import net.neoforged.neoforge.common.world.BiomeModifiers;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import org.zecontech.extraplus.init.ModEntities;
import org.zecontech.extraplus.util.Helpers;

/**
 * 模组生物群系修改数据生成提供者
 */
public class ModBiomeModiferProvider {

    public static final ResourceKey<BiomeModifier> ADD_KELONG =
            register("add_kelong");
    public static final ResourceKey<BiomeModifier> ADD_ORE_RUBY =
            register("add_ore_ruby");
    public static final ResourceKey<BiomeModifier> ADD_ORE_AMBER =
            register("add_ore_amber");
    public static final ResourceKey<BiomeModifier> ADD_ORE_RUBY_LOWER =
            register("add_ore_ruby_lower");
    public static final ResourceKey<BiomeModifier> ADD_PLANTAGO =
            register("add_plantago");
    public static final ResourceKey<BiomeModifier> ADD_ACHILLEA_WHITE =
            register("add_achillea_white");
    public static final ResourceKey<BiomeModifier> ADD_ZOMBIE_HORSE =
            register("add_zombie_horse");
    public static final ResourceKey<BiomeModifier> ADD_SKELETON_HORSE =
            register("add_skeleton_horse");
    public static final ResourceKey<BiomeModifier> ADD_ANTIRRHINUM =
            register("add_antirrhinum");
    public static final ResourceKey<BiomeModifier> ADD_CAMPANULA_BLUE =
            register("add_campanula_blue");
    public static final ResourceKey<BiomeModifier> ADD_CENTAUREA =
            register("add_centaurea");
    public static final ResourceKey<BiomeModifier> ADD_DIGITALIS_BLUE =
            register("add_digitalis_blue");
    public static final ResourceKey<BiomeModifier> ADD_DIGITALIS_YELLOW =
            register("add_digitalis_yellow");
    public static final ResourceKey<BiomeModifier> ADD_DOSERA =
            register("add_dosera");
    public static final ResourceKey<BiomeModifier> ADD_IRIS =
            register("add_iris");
    public static final ResourceKey<BiomeModifier> ADD_LEONTOPODIUM_WHITE =
            register("add_leontopodium_white");
    public static final ResourceKey<BiomeModifier> ADD_LEUCANTHEMUM_WHITE =
            register("add_leucanthemum_white");
    public static final ResourceKey<BiomeModifier> ADD_MUSHROOM_AMANITA_GREEN =
            register("add_mushroom_amanita_green");
    public static final ResourceKey<BiomeModifier> ADD_MUSHROOM_BOLETUS =
            register("add_mushroom_boletus");
    public static final ResourceKey<BiomeModifier> ADD_MUSHROOM_CANTHARELLUS =
            register("add_mushroom_cantharellus");
    public static final ResourceKey<BiomeModifier> ADD_MUSHROOM_GALERINA =
            register("add_mushroom_galerina");
    public static final ResourceKey<BiomeModifier> ADD_MUSHROOM_GYROMITRA =
            register("add_mushroom_gyromitra");
    public static final ResourceKey<BiomeModifier> ADD_MUSHROOM_MACROLEPIOTA =
            register("add_mushroom_macrolepiota");
    public static final ResourceKey<BiomeModifier> ADD_PAPVER_RED =
            register("add_papver_red");
    public static final ResourceKey<BiomeModifier> ADD_TRIFOLIUM_RED =
            register("add_trifolium_red");
    public static final ResourceKey<BiomeModifier> ADD_TRIFOLIUM_WHITE =
            register("add_trifolium_white");

    /**
     * 添加生物群系修改数据
     * @param context 模组修改内容
     */
    public static void addBiomeModifiers(BootstrapContext<BiomeModifier> context) {
        HolderGetter<Biome> biomes = context.lookup(Registries.BIOME);
        HolderGetter<PlacedFeature> features = context.lookup(Registries.PLACED_FEATURE);
        context.register(ADD_KELONG, new BiomeModifiers.AddSpawnsBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_JUNGLE),
                WeightedList.<MobSpawnSettings.SpawnerData>builder().add(new Weighted<>(new MobSpawnSettings.SpawnerData(ModEntities.KELONG.get(),1, 2), 12)).build()
        ));
        context.register(ADD_ORE_RUBY, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.ORE_RUBY)),
                GenerationStep.Decoration.UNDERGROUND_ORES
        ));
        context.register(ADD_ORE_RUBY_LOWER, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.ORE_RUBY_LOWER)),
                GenerationStep.Decoration.UNDERGROUND_ORES
        ));
        context.register(ADD_PLANTAGO, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.PLANTAGO)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_ACHILLEA_WHITE, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.ACHILLEA_WHITE)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_ZOMBIE_HORSE, new BiomeModifiers.AddSpawnsBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                WeightedList.<MobSpawnSettings.SpawnerData>builder().add(new Weighted<>(new MobSpawnSettings.SpawnerData(EntityType.ZOMBIE_HORSE,1, 2), 12)).build()
        ));
        context.register(ADD_SKELETON_HORSE, new BiomeModifiers.AddSpawnsBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                WeightedList.<MobSpawnSettings.SpawnerData>builder().add(new Weighted<>(new MobSpawnSettings.SpawnerData(EntityType.SKELETON_HORSE,1, 2), 12)).build()
        ));
        context.register(ADD_ORE_AMBER, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_NETHER),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.ORE_AMBER)),
                GenerationStep.Decoration.UNDERGROUND_ORES
        ));
        context.register(ADD_ANTIRRHINUM, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.ANAGALLIS_YELLOW)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_CAMPANULA_BLUE, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.CAMPANULA_BLUE)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_CENTAUREA, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.CENTAUREA)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_DIGITALIS_BLUE, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.DIGITALIS_BLUE)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_DIGITALIS_YELLOW, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.DIGITALIS_YELLOW)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_DOSERA, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.DOSERA)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_IRIS, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.IRIS)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_LEONTOPODIUM_WHITE, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.LEONTOPODIUM_WHITE)),
                GenerationStep.Decoration.VEGETAL_DECORATION));
        context.register(ADD_LEUCANTHEMUM_WHITE, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.LEUCANTHEMUM_WHITE)),
                GenerationStep.Decoration.VEGETAL_DECORATION));
        context.register(ADD_MUSHROOM_AMANITA_GREEN, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_JUNGLE),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.MUSHROOM_AMANITA_GREEN)),
                GenerationStep.Decoration.VEGETAL_DECORATION));
        context.register(ADD_MUSHROOM_BOLETUS, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_JUNGLE),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.MUSHROOM_BOLETUS)),
                GenerationStep.Decoration.VEGETAL_DECORATION));
        context.register(ADD_MUSHROOM_CANTHARELLUS, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_JUNGLE),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.MUSHROOM_CANTHARELLUS)),
                GenerationStep.Decoration.VEGETAL_DECORATION));
        context.register(ADD_MUSHROOM_GALERINA, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_JUNGLE),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.MUSHROOM_GALERINA)),
                GenerationStep.Decoration.VEGETAL_DECORATION));
        context.register(ADD_MUSHROOM_GYROMITRA, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_JUNGLE),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.MUSHROOM_GYROMITRA)),
                GenerationStep.Decoration.VEGETAL_DECORATION));
        context.register(ADD_MUSHROOM_MACROLEPIOTA, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_JUNGLE),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.MUSHROOM_MACROLEPIOTA)),
                GenerationStep.Decoration.VEGETAL_DECORATION));
        context.register(ADD_PAPVER_RED, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.PAPVER_RED)),
                GenerationStep.Decoration.VEGETAL_DECORATION));
        context.register(ADD_TRIFOLIUM_RED, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.TRIFOLIUM_RED)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        context.register(ADD_TRIFOLIUM_WHITE, new BiomeModifiers.AddFeaturesBiomeModifier(
                biomes.getOrThrow(Tags.Biomes.IS_OVERWORLD),
                HolderSet.direct(features.getOrThrow(ModPlacedFeatureProvider.TRIFOLIUM_WHITE)),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
    }

    /**
     * 注册新的资源键
     * @param name 名字
     * @return 资源键
     */
    private static ResourceKey<BiomeModifier> register(String name) {
        return ResourceKey.create(NeoForgeRegistries.Keys.BIOME_MODIFIERS, Helpers.identifier(name));
    }
}
